package usaco.c2009.november.bronze;

import java.util.Queue;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;

/*
 * p3458
 * USACO 2009 November - Bronze
 */
public class p3458 {

	static int C, R;
	static char[][] m;
	static pos K;
	static Queue<pos> qu;
	static int[] rm = {-2,-2,-1,-1,1,1,2,2};
	static int[] cm = {-1,1,-2,2,-2,2,-1,1};
	
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		C = scanner.nextInt();
		R = scanner.nextInt();
		m = new char[R+1][C+1];
		for (int i=1;i<=R;++i) {
			String s = scanner.next();
			for (int j=1;j<=C;++j) {
				m[i][j] = s.charAt(j-1);
				if (m[i][j] == 'K') {
					K = new pos(i,j,0);
				}
			}
		}
		qu = new ArrayBlockingQueue<pos>(40000);
		qu.add(K);
		m[K.r][K.c] = 'V';
		while (!qu.isEmpty()) {
			pos p = qu.poll();
			for (int i=0;i<rm.length;++i) {
				int nr = p.r + rm[i];
				int nc = p.c + cm[i];
				if (nr >= 1 && nr <= R && nc >= 1 && nc <= C) {
					if (m[nr][nc] == '.') {
						qu.add(new pos(nr,nc,p.deep+1));
						m[nr][nc] = 'V';
					}
					else if (m[nr][nc] == 'H') {
						System.out.println(p.deep+1);
						return;
					}
				}
			}
		}
	}

}
class pos {
	int r;
	int c;
	int deep;
	pos(int r, int c, int deep) {
		this.r = r;
		this.c = c;
		this.deep = deep;
	}
}